package org.glut.cloud.repairManage.controller;

import lombok.extern.slf4j.Slf4j;
import org.glut.cloud.repairManage.entity.FailureRepair;
import org.glut.cloud.repairManage.entity.MaintenanceRecords;
import org.glut.cloud.repairManage.service.IFailureRepairPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.List;

/**
 *  前端控制器
 *
 * @author stubborn
 * @since 2024-08-13 15:30
 */

@CrossOrigin
@RestController
@Slf4j
public class FailureRepairPostController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    IFailureRepairPostService failureRepairPoseService;

    @PostMapping("/failure-repair/save")  //前端：axios.post()
    public boolean save(@RequestBody FailureRepair failureRepair) { //@RequestBody将请求的JSON数据解析成POJO对象

        return failureRepairPoseService.save(failureRepair);

    }

    @PostMapping("/maintenance-record/save")
    public ResponseEntity<Boolean> saveMaintenanceRecord(@RequestBody MaintenanceRecords maintenanceRecord) {
        // 通过 RestTemplate 方式调用远程服务
        ResponseEntity<Boolean> entity = restTemplate.postForEntity(
                "http://repair-RepairRecordManage/maintenance-records/save",
                maintenanceRecord,
                Boolean.class);

        return entity;
    }

    @GetMapping("/maintenance-record/user/{userNo}")
    public ResponseEntity<List<MaintenanceRecords>> getMaintenanceRecordsByUserNo(@PathVariable Integer userNo) {
        try {
            // 通过 RestTemplate 方式调用远程服务
            ResponseEntity<List<MaintenanceRecords>> response = restTemplate.exchange(
                    "http://repair-RepairRecordManage/maintenance-records/user/{userNo}",
                    HttpMethod.GET,
                    null,
                    new ParameterizedTypeReference<List<MaintenanceRecords>>() {},
                    userNo
            );
            return response;
        } catch (HttpClientErrorException | HttpServerErrorException e) {
            log.error("Error fetching maintenance records for user {}: ", userNo, e);
            return ResponseEntity.status(e.getStatusCode())
                    .body(Collections.emptyList());
        } catch (Exception e) {
            log.error("Unexpected error while fetching maintenance records for user {}: ", userNo, e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Collections.emptyList());
        }
    }



    @GetMapping("/failure-repair/selectAll")
    public List<FailureRepair> selectAll() {
        return failureRepairPoseService.selectAll();
    }

    @GetMapping("/failure-repair/select/{userNo}")
    public List<FailureRepair> selectByUserNo(@PathVariable String userNo) {
        return failureRepairPoseService.selectByUserNo(userNo);

    }

    @PutMapping("/failure-repair/update")
    public boolean updateFailureRepair(@RequestBody FailureRepair failureRepair) {
        return failureRepairPoseService.updateFailureRepair(failureRepair);
    }

    @DeleteMapping("/failure-repair/delete/{id}")
    public boolean deleteFailureRepair(@PathVariable Integer id) {
        return failureRepairPoseService.deleteFailureRepair(id);
    }

}
